package com.longtop.leaning.util;

import java.io.UnsupportedEncodingException;


public class CharacterUtil {
	
	/*
	 * 将中文字转换成16进制编码
	 * Create by haicao
	 * 2009-08-15
	 */
	public static String bytesToHexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer(2 * buf.length);
		for (int i = 0; i < buf.length; i++) {
			int h = (buf[i] & 0xf0) >> 4;
			int l = (buf[i] & 0x0f);
			sb.append(new Character((char) ((h > 9) ? 'a' + h - 10 : '0' + h)));
			sb.append(new Character((char) ((l > 9) ? 'a' + l - 10 : '0' + l)));
		}
		return sb.toString().toUpperCase();
	}
	
	/*
	 * 将中文字转换成16进制编码
	 * Create by haicao
	 * 2009-08-15
	 */
	public static byte[] strToByteArr(String str) {
		byte[] buf = null;
		byte[] result = null;
		try {
			buf = str.getBytes("gb2312");
		} catch (UnsupportedEncodingException e) {
			buf = null;
			e.printStackTrace();
		}
		
		if (buf == null)
			return result;
		
		result = new byte[buf.length];
		int j = 0;
		for (int i = 0; i < buf.length; i++) {
			int h = (buf[i] & 0xf0) >> 4;
			int l = (buf[i] & 0x0f);
			System.out.println("高位：" + (char) ((h > 9) ? 'a' + h - 10 : '0' + h));
			System.out.println("低位：" + (char) ((l > 9) ? 'a' + l - 10 : '0' + l));
			String temp = String.valueOf((char)((h > 9) ? 'a' + h - 10 : '0' + h)).toUpperCase() 
			+  String.valueOf((char)((l > 9) ? 'a' + l - 10 : '0' + l)).toUpperCase();
			System.out.println("值：" + temp);
			// 高位
			//String hStr = String.valueOf((char)((h > 9) ? 'a' + h - 10 : '0' + h)).toUpperCase();
			// 低位
			//String lStr = String.valueOf((char)((l > 9) ? 'a' + l - 10 : '0' + l)).toUpperCase();
			//result[i] = Byte.parseByte(temp);
			result[i] = (byte) Integer.parseInt(temp, 16);
			//System.out.println("值:" + Integer.parseInt(temp, 16));
			//result[j] = Byte.parseByte(hStr, 16);
			//result[j + 1] = Byte.parseByte(lStr, 16);
			//j += 2;
			
		}
		return result;
	}
	/**
	 * 将16进制字符串转换成byte数组
	 * @param str
	 * @return
	 * Create by haicao
	 * 2009-08-15
	 */
	public static byte[] hex2byte(String str) {
		if (str == null)
			return null;
		str = str.trim();
		int len = str.length();
		if (len == 0 || len % 2 == 1)
			return null;

		byte[] b = new byte[len / 2];
		try {
			for (int i = 0; i < str.length(); i += 2) {
				/*
				b[i / 2] = (byte) Integer.
						.decode("0x" + str.substring(i, i + 2)).intValue();
						*/
				// Integer.valueOf 第二个参数默认为对10进制字串的转换,16表示值为16进制字串
				b[i / 2] = (byte) Integer.valueOf(str.substring(i, 
						i + 2), 16).intValue();
			}
			return b;
		} catch (Exception e) {
			return null;
		}
	}
	
	/*
	 * 将中文字符转码为字符串
	 * Create by haicao
	 * 2009-7-13
	 * 数据编码 为 GB2312 正常运行
	 */
	public static String chineseEncode(String s) {
		String result = "";
		if (StringUtils.isEmpty(s.trim()))
			return "";
		try {
			//result = new String(s.getBytes(), "UTF-8");
			//result = new String(s.getBytes("UTF-8"),"UTF-8"); 
			result = s;
			result = bytesToHexStr(result.getBytes("GB2312"));
		} catch (UnsupportedEncodingException e) {
			result = "";
			e.printStackTrace();
		}
		System.out.println("---------编码后-----------：" + result);
		return result;
	}
	
	/*
	 * 将16进制字符串转码为字符串(支持中文)
	 * Create by haicao
	 * 2009-7-13
	 */
	public static String chineseDecode(String s) {
		try {
			return new String(hex2byte(s), "GB2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * 加密字符串
	 * @param key
	 * @return
	 */
	public static String encrpPasswd(String key) {
		String result = "";
		if (key == null || "".equals(key.trim()))
			return "";
		result = Base64.encode(key.getBytes());
		result = chineseEncode(result);
		return result;
		
	}
	
	/**
	 * 解密字符串
	 * @param key
	 * @return
	 */
	public static String decrpPasswd(String key) {
		String result = "";
		if (key == null || "".equals(key.trim()))
			return "";
		result = chineseDecode(key);
		result = new String(Base64.decode(result));
		return result;
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		String str = "厦门\r\n";
		String result = chineseEncode(str);
		System.out.println("编码后：" + result);
		System.out.println(bytesToHexStr(str.getBytes()));
		/**
		byte[] arr = str.getBytes();
		for (int i = 0; i < arr.length; i ++) {
			int h = (arr[i] & 0xf0) >> 4;
			int l = (arr[i] & 0x0f);
			char c = (char) ((h > 9) ? 'a' + h - 10 : '0' + h);
			char d = (char) ((h > 9) ? 'a' + h - 10 : '0' + h);
			System.out.println("高位:" + c + " 低位:" + d);
		}
		*/
		byte[] bytearr = strToByteArr("厦门");
		System.out.println("长度" + bytearr.length);
		for (int i = 0; i < bytearr.length; i ++) {
			System.out.println("" + bytearr[i]);
		}
		String source = chineseDecode(result);
		System.out.println("解码后：" + source);
		System.out.println("值" + Byte.parseByte("31", 16));
	}
}
